(function () {
  var stateMap, elemMap, setElemMap, bindElemMap, handleClickSure;

  handleClickSure = function () {
    if (Number(elemMap.codeIpt.value).toString().length !== 6) {
      util.toast('请输入正确的验证码！');
      return false;
    }

    if (!util.validatePwd(elemMap.pwdIpt.value)) {
      util.toast('请输入正确格式的密码！');
      return false;
    }

    if (elemMap.pwdIpt.value !== elemMap.pwdAgainIpt.value) {
      util.toast('两次密码不一致！');
      return false;
    }

    //禁止多次点击提交
    elemMap.signNow.style.pointerEvents = 'none';
    util.addFetchLoading();

    $.ajax({
      url: util.hostUrl + '/v1/userInfo/password.html',
      type: 'post',
      data: {
        header: JSON.stringify({
          reqKey: util.reqKey,
          cookie: localStorage.jqyCookie,
          token: localStorage.jqyToken
        }),
        param: JSON.stringify({
          app: util.app,
          phone: localStorage.jqyPhone,
          pass: elemMap.pwdIpt.value,
          code: elemMap.codeIpt.value
        })
      },
      success: function (res) {
        if (res.result.code == 1) {
          util.toast('修改密码成功！');
          setTimeout(function () {
            location.replace('./setting.html');
          }, 2600);
        } else if (res.result.code == 3) {
          util.toast('未获取验证码！');
        } else if (res.result.code == 4) {
          util.toast('验证码失效！');
        } else if (res.result.code == 5) {
          util.toast('验证码不匹配！');
        } else if (res.result.code == 6) {
          util.toast('该用户没有注册！');
        } else {
          util.toast('Oops,修改密码失败！');
        }
      },
      error: function () {
        util.toast('Oops,网络有问题！');
      },
      complete: function () {
        elemMap.signNow.style.pointerEvents = 'auto';
        util.removeFetchLoading();
      }
    });
  };

  bindElemMap = function () {
    elemMap.pwdIpt.addEventListener('keyup', function () {
      util.showOrHidePreviewBtn(this, this.parentNode.querySelector('.ipt-eye'));
    });
    elemMap.pwdAgainIpt.addEventListener('keyup', function () {
      util.showOrHidePreviewBtn(this, this.parentNode.querySelector('.ipt-eye'));
    });
    elemMap.codeIpt.addEventListener('keyup', function () {
      util.showOrHideClearBtn(this, this.parentNode.querySelector('.ipt-clear'));
    });

    util.nList2Arr(document.querySelectorAll('.ipt-clear')).forEach(function (btn) {
      btn.addEventListener('click', function () {
        util.clearIpt(this.parentNode.querySelector('input'), this);
      });
    });

    util.nList2Arr(document.querySelectorAll('.ipt-eye')).forEach(function (btn) {
      btn.addEventListener('click', function () {
        util.changePwdState(this.parentNode.querySelector('input'), this);
      });
    });

    elemMap.getCode.addEventListener('click', function () {
      util.fetchCode(this, localStorage.jqyPhone, 2);
    });

    elemMap.signNow.addEventListener('click', handleClickSure);
  };

  setElemMap = function () {
    elemMap = {
      pwdIpt      : document.querySelector('#pwdIpt'),
      pwdAgainIpt : document.querySelector('#pwdAgainIpt'),
      codeIpt     : document.querySelector('#codeIpt'),
      getCode     : document.querySelector('.get-code'),
      signNow     : document.querySelector('.sign-now')
    };
  };

  setElemMap();
  bindElemMap();
  util.setPageHeight();
}());


